﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.Common.Utils;
using Payroll.BusinessLogic;
using Payroll.BusinessLogic.Directory;
using Payroll.BusinessLogic.CustomerManager;
using PayrollHR.Web;
using System.Data;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.Utils;

namespace PayrollHR.Administration.Modules
{
    public partial class CaseProfileFinancialSource : BasePayrollAdministrationUserControl
    {
        public int CustomerID
        {
            get
            {
                return CommonHelper.QueryStringInt("CustomerId");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<ProfileFinan> customer = CustomerService.GetAllByCustomerId(this.CustomerID);
                BindGrid(customer);
            }
        }
        protected void BindGrid(List<ProfileFinan> dt)
        {
            ViewState["CaseProfileFinancialSource"] = dt;
            gvObject.DataSource = dt;
            gvObject.DataBind();
        }
        protected void gvObject_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvObject.PageIndex = e.NewPageIndex;
            List<ProfileFinan> customer = CustomerService.GetAllByCustomerId(this.CustomerID);
            BindGrid(customer);
        }
        private List<ProfileFinan> getTableGridview()
        {
            List<ProfileFinan> dtCurrentTable = new List<ProfileFinan>();
            if (ViewState["CaseProfileFinancialSource"] != null)
            {
                dtCurrentTable = (List<ProfileFinan>)ViewState["CaseProfileFinancialSource"];
            }
            return dtCurrentTable;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            List<ProfileFinan> listProfile = getTableGridview();
            ProfileFinan profile = new ProfileFinan()
            {
                ProfileFinanID = -1,
                PaperRequest = txtPaperRequest.Text,
                Content = txtContent.Text,
                OnDate = dpOnDate.SelectText,
                WHoDo = txtWHoDo.Text,
                Postage = txtPostage.Value,
                ProComplete = txtProComplete.Text,
                UpdateByID = 1,
                CreateByID = 1,
                CreatedOn = Convert.ToDateTime(DateTime.Today),
                UpdatedOn = Convert.ToDateTime(DateTime.Today)
            };
            listProfile.Add(profile);
            BindGrid(listProfile);
        }

        protected void gvObject_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton btnDelete = e.Row.FindControl("lbDelete") as LinkButton;
                if (btnDelete != null)
                    btnDelete.CommandArgument = e.Row.RowIndex.ToString();
            }
        }

        protected void gvObject_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int index = e.RowIndex;
                GridViewRow row = gvObject.Rows[index];
                HiddenField hfProfileID = row.FindControl("hfProfileID") as HiddenField;
                List<ProfileFinan> listProfile = getTableGridview();
                if (listProfile != null)
                {
                    listProfile.RemoveAt(index);
                    if (CommonHelper.ToInteger(hfProfileID.Value) > 0)
                    {
                        List<int> List = new List<int>();
                        List.Add(CommonHelper.ToInteger(hfProfileID.Value));
                        this.CustomerService.DeleteProfileFinan(List);
                    }
                    BindGrid(listProfile);
                }
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }
        public List<ProfileFinan> GetProfileFinan()
        {
            if (ViewState["CaseProfileFinancialSource"] != null)
            {
                List<ProfileFinan> dt = (List<ProfileFinan>)ViewState["CaseProfileFinancialSource"];
                return dt;
            }
            else
            {
                return new List<ProfileFinan>();
            }
        }
    }
}